/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.thinktank.service.mapstruct;

import com.fzhucc.base.BaseMapper;
import com.fzhucc.modules.system.service.UserService;
import com.fzhucc.modules.system.service.dto.UserDto;
import com.fzhucc.modules.system.service.dto.UserSmallDto;
import com.fzhucc.modules.system.service.mapstruct.UserMapper;
import com.fzhucc.modules.thinktank.domain.Academician;
import com.fzhucc.modules.thinktank.domain.Capital;
import com.fzhucc.modules.thinktank.domain.dto.AcademicianDto;
import com.fzhucc.modules.thinktank.domain.dto.CapitalDto;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.ReportingPolicy;
import org.springframework.beans.factory.annotation.Autowired;

/**
* @website https://eladmin.vip
* @author Woolsen
* @date 2022-11-14
**/
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public abstract class CapitalMapper implements BaseMapper<CapitalDto, Capital> {
    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    @Override
    @Mapping(target = "user", source = "userId", qualifiedByName = "userDtoToUserSmallDto")
    public abstract CapitalDto toDto(Capital entity);

    @Named("userDtoToUserSmallDto")
    protected UserSmallDto toSmallDto(Long userId) {
        try {
            UserDto userDto = userService.findById(userId);
            return userMapper.toSmallDto(userDto);
        } catch (Exception e) {
            return null;
        }
    }
}